package com.epro.dx.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;

import com.weipu.common.constants.Constant;

import java.io.Serializable;

/**
 * 意图 传递数据工具类
 *
 * @author Zenglw
 * @version [版本号, 2017-6-12]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class IntentUtils {

    public static void launchActivityByAction(Context context,String action,Uri uri) {
        Intent intent =  new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,uri);
        context.startActivity(intent);
    }

    public static void launchActivity(Activity activity, Class cls) {
        IntentUtils.launchActivity(activity, cls, null);
    }

    public static void launchActivity(Activity activity, Class cls, Bundle param) {
        Intent intent = new Intent(activity, cls);
        if (param != null) {
            intent.putExtra(Constant.Intent.INTENT_PUT_DATA, param);
        }
        activity.startActivity(intent);
    }

    public static void launchActivityForResult(Activity activity, Class cls, int requestCode) {
        IntentUtils.launchActivityForResult(activity, cls, requestCode, null);
    }

    public static void launchActivityForResult(Activity activity, Class cls, int requestCode, Bundle params) {

        Intent intent = new Intent(activity, cls);
        if (params != null) {
            intent.putExtra(Constant.Intent.INTENT_PUT_DATA, params);
        }
        activity.startActivityForResult(intent, requestCode);

    }

    public static void launchActivityForResult(Fragment fragment, Class cls, int requestCode) {
        IntentUtils.launchActivityForResult(fragment, cls, requestCode, null);
    }

    public static void launchActivityForResult(Fragment fragment, Class cls, int requestCode, Bundle params) {

        Intent intent = new Intent(fragment.getActivity(), cls);
        if (params != null) {
            intent.putExtra(Constant.Intent.INTENT_PUT_DATA, params);
        }
        fragment.startActivityForResult(intent, requestCode);

    }

    public static void jumpToPage(Context context,String appPackageName) {
        Uri packageURI = Uri.parse("package:" + appPackageName);
        Intent intent =  new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,packageURI);
        context.startActivity(intent);
    }


    public static int getIntParam(Intent intent, String field, int defValue) {
        Bundle bundleExtra = intent.getBundleExtra(Constant.Intent.INTENT_PUT_DATA);
        if (bundleExtra != null) {
            return bundleExtra.getInt(field, defValue);
        } else {
            return defValue;
        }
    }

    public static double getDoubleParam(Intent intent, String field, double defValue) {
        Bundle bundleExtra = intent.getBundleExtra(Constant.Intent.INTENT_PUT_DATA);
        if (bundleExtra != null) {
            return bundleExtra.getDouble(field, defValue);
        } else {
            return defValue;
        }
    }

    public static boolean getBooleanParam(Intent intent, String field, boolean defValue) {
        Bundle bundleExtra = intent.getBundleExtra(Constant.Intent.INTENT_PUT_DATA);
        if (bundleExtra != null) {
            return bundleExtra.getBoolean(field, defValue);
        } else {
            return defValue;
        }
    }

    public static Serializable getSerializable(Intent intent, String field) {
        Bundle bundleExtra = intent.getBundleExtra(Constant.Intent.INTENT_PUT_DATA);
        if (bundleExtra != null) {
            return bundleExtra.getSerializable(field);
        } else {
            return null;
        }
    }

    public static String getString(Intent intent, String field, String defValue) {
        Bundle bundleExtra = intent.getBundleExtra(Constant.Intent.INTENT_PUT_DATA);
        if (bundleExtra != null) {
            return bundleExtra.getString(field, defValue);
        } else {
            return null;
        }
    }

}
